<script setup lang="ts">
import { ref } from "vue";
import {
  ElMessage,
  genFileId,
  UploadInstance,
  UploadProps,
  UploadRawFile,
  UploadUserFile,
} from "element-plus";
import { uploadVideoURL } from "../../api/section.ts";
import useSectionStore from "../../store/sectionStore.ts";

const upload = ref<UploadInstance>();
const emits = defineEmits(["upload-already"]);

const title = ref<string>("");

const sectionStore = useSectionStore();

interface ChapterQuestion {
  title: string;
  index: number;
}

interface SectionQuestion {
  id: number;
  title: string;
}

const chapters = ref<ChapterQuestion[]>([]);
const sections = ref<[item?: SectionQuestion[]]>([]);

const chapter = ref();
const section = ref();

sectionStore.getAllChapter().then(() => {
  sectionStore.chapters.forEach((item, index) => {
    chapters.value.push({
      title: item.title,
      index,
    });
    sections.value.push([]);
    item.sections.forEach((it) => {
      sections.value[index].push({
        id: it.id,
        title: it.title,
      });
    });
  });
  // console.log(chapters, sections);
});

const handleChapterChange = () => {
  section.value = null;
};

const handleExceed: UploadProps["onExceed"] = (files) => {
  upload.value!.clearFiles();
  const file = files[0] as UploadRawFile;
  file.uid = genFileId();
  upload.value!.handleStart(file);
};

const handleChange: UploadProps["onChange"] = (uploadFile, uploadFiles) => {
  const fileType = uploadFile.name.substring(uploadFile.name.lastIndexOf("."));
  if (fileType.toLowerCase() !== ".mp4") {
    ElMessage({
      message: "请上传mp4文件",
      type: "error",
    });
    uploadFiles.pop();
  }
};

const handleUploadSuccess = (response: any) => {
  if (response.data) {
    ElMessage({
      message: "上传资料成功",
      type: "success",
    });
    fileList.value.pop();
    emits("upload-already");
  } else {
    ElMessage.error(`上传失败: ${response.message}`);
  }
};

const beforeUploadFile = () => {
  if (section.value && title.value !== "" && fileList.value.length > 0) {
    upload.value?.submit();
    // console.log(upload.value);
  } else {
    ElMessage.error("上传失败");
  }
};

const fileList = ref<UploadUserFile[]>([]);
</script>

<template>
  <el-form :label-position="'top'">
    <el-form-item label="视频标题: ">
      <el-input type="text" v-model="title" />
    </el-form-item>
    <el-form-item label="选择小节">
      <el-select v-model="chapter" @change="handleChapterChange">
        <el-option
          v-for="t_chapter in chapters"
          :key="t_chapter.index"
          :label="t_chapter.title"
          :value="t_chapter.index"
        />
      </el-select>
      <el-select v-model="section" class="ml-3">
        <el-option
          v-for="t_section in sections[chapter]"
          :key="t_section.id"
          :label="t_section.title"
          :value="t_section.id"
        />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-upload
        :data="{
          title,
          section,
        }"
        v-model:file-list="fileList"
        :on-change="handleChange"
        :action="uploadVideoURL"
        :auto-upload="false"
        :limit="1"
        ref="upload"
        :on-exceed="handleExceed"
        :on-success="handleUploadSuccess"
      >
        <template #trigger>
          <el-button type="primary">选择文件</el-button>
        </template>
        <template #tip> <div>请上传一个视频文件</div> </template>
      </el-upload>
    </el-form-item>
    <el-button type="success" @click="beforeUploadFile">点击上传</el-button>
  </el-form>
</template>

<style scoped></style>
